package com.ruoyi.web.controller.wexin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.weixin.Exception.BussnissExecption;
import com.ruoyi.weixin.entity.*;
import com.ruoyi.weixin.service.FrentsService;
import com.ruoyi.weixin.service.UserService;
import com.ruoyi.weixin.service.WenDaService;
import com.ruoyi.weixin.untils.MD5;
import com.ruoyi.weixin.vo.RePassvO;
import com.ruoyi.weixin.vo.UserPassWord;
import com.ruoyi.weixin.vo.UserVo;
import com.ruoyi.weixin.vo.WenVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;

@RestController
@RequestMapping("/wexinLogin")
public class LoginWeixinController {

    @Autowired
    UserService userInfoService;

    @Autowired
    WenDaService wenDaService;

    @Autowired
    FrentsService frentsService;
    @PostMapping("/updateUser")
    public R<?> updateUser(@RequestBody User user) {
        if (user.getPassWord()!=null){
            user.setPassWord(MD5.encrypt(user.getPassWord()));
        }
        Frents frents = new Frents();
        frents.setUserName(user.getNickName());
        boolean update = frentsService.update(frents,new LambdaQueryWrapper<Frents>().eq(Frents::getUserId, user.getId()));

        boolean save = userInfoService.updateById(user);
        return R.ok(save);
    }

    @PostMapping("/setTab")
    public R<?> setTab(String userId ,String tab) {
        if (StringUtils.isEmpty(userId)||StringUtils.isEmpty(tab)){
            throw  new BussnissExecption("参数不能为空");
        }
        User user = new User();
        user.setId(Integer.parseInt(userId));user.setUserTab(tab);
        return R.ok(userInfoService.updateById(user));
    }

    @PostMapping("/getUser")
    public R<?> getUser(String userId ) {
        return R.ok(userInfoService.getById(userId));
    }

    @PostMapping("/login")
    public R<Map<String,Object>> loginCheck(@RequestBody WeChatModel weChatModel, HttpServletResponse response, HttpServletRequest request) {
       return R.ok(userInfoService.checkLogin(weChatModel.getCode(),request));
    }


    @PostMapping("/passwordLogin")
    public R<Map<String,Object>> passwordLogin(@RequestBody UserPassWord userVo,HttpServletRequest request) {
        return R.ok( userInfoService.checkLoginByPassword(userVo,request));
    }

    @GetMapping("/findPassByda")
    public R<?> wenFindPassByda(UserVo userVo) {
        return R.ok( userInfoService.wenFindPassByda(userVo));
    }

    @GetMapping("/findWenda")
    public R<?> findWenda(String phone) {
        User one = userInfoService.getOne(new LambdaQueryWrapper<User>().eq(User::getPhone, phone));
        if (one==null){
            throw new BussnissExecption("该用户未注册");
        }
        List<WenDa> list = wenDaService.list(new LambdaQueryWrapper<WenDa>().eq(WenDa::getUserId,one.getId()).orderByAsc(WenDa::getId));
        if (list.isEmpty()){
            throw new BussnissExecption("该用户未曾开通问答找回");
        }
        return R.ok(list);
    }
    @GetMapping("/checkUser")
    public R<?> checkUser(String phone) {
        User one = userInfoService.getOne(new LambdaQueryWrapper<User>().eq(User::getPhone, phone));
        if (one==null){
            throw new BussnissExecption("该用户未注册");
        }
        return R.ok();
    }

    @GetMapping("/checkwenda")
    public R<?> checkwenda(String phone,List<WenDa> list) {
        if (list==null){
            throw new BussnissExecption("账户错误");
        }
        User one = userInfoService.getOne(new LambdaQueryWrapper<User>().eq(User::getPhone, phone));
        if (one==null){
            throw new BussnissExecption("账户错误");
        }
        List<WenDa> wenDas = wenDaService.list(new LambdaQueryWrapper<WenDa>().eq(WenDa::getUserId,one.getId()));
        if (wenDas.isEmpty()){
            throw new BussnissExecption("用户未填写问题");
        }
         if (!(list.containsAll(wenDas)&&wenDas.containsAll(list))){
             throw new BussnissExecption("信息错误");
         }
        return R.ok();
    }

    @GetMapping("/sentEmail")
    public R<?> sentEmail(String phone) {
        return R.ok(userInfoService.sentEmail(phone));
    }
    @GetMapping("/checkCode")
    public R<?> checkCode(String code) {
        return userInfoService.checkCode(code);
    }

    @PostMapping("/reSetPass")
    public R<?> reSetPass(@RequestBody RePassvO rePassvO) {
        return userInfoService.reSetPass(rePassvO);
    }

    @PostMapping("/wenvo")
    public R<?> wenvo(@RequestBody WenVo wenVo) {
        LambdaQueryWrapper<WenDa> wrapper = new LambdaQueryWrapper<>();
        List<WenDa> list1 = wenDaService.list(wrapper.eq(WenDa::getUserId, wenVo.getUserId()));
        if (!list1.isEmpty()){
            wenDaService.remove(wrapper.eq(WenDa::getUserId, wenVo.getUserId()));
        }
        ArrayList<String> list = new ArrayList<>();
        list.add(wenVo.getWen1());    list.add(wenVo.getWen2());    list.add(wenVo.getWen3());
        WenDa wenDa = new WenDa();
        wenDa.setUserId(wenVo.getUserId());
        for (int i = 0; i < list.size(); i++) {
            wenDa.setId(null);
            if (i==0){
                wenDa.setWenName("您童年的绰号叫什末？");
                wenDa.setDaName(list.get(0));
                wenDaService.save(wenDa);
            }else if (i==1){
                wenDa.setWenName("不能说的秘密？");
                wenDa.setDaName(list.get(1));
                wenDaService.save(wenDa);
            }else {
                wenDa.setWenName("不能忘记的事情");
                wenDa.setDaName(list.get(2));
                wenDaService.save(wenDa);
            }
        }
        return R.ok();
    }

}
